import { Blob2Base64, Object2Blob } from "@/utils";
import * as giteeService from "@/data/service/giteeService";
import { ElNotification } from "element-plus";
import { useAccountStore } from "@/data/stores/account";

const accountStore = useAccountStore()

export function set(fileName:string, content:any){

    if (accountStore.getGitee().access_token == ''){
        return
    }

    const blob = Object2Blob(content, 'application/json')
    Blob2Base64(blob, (dataUrl:string)=>{
        const base64 = dataUrl.split(',')[1]
        giteeService.updateFile(
            fileName,
            base64,
            '备份'
        ).then(rs=>{
            ElNotification({
                title: '成功上传至gitee',
                message: '成功上传至gitee',
                type: 'success',
                duration: 800
            })
        }).catch(err=>{
            ElNotification({
                title: '自动备份失败',
                message: err,
                type: 'error',
                duration: 0,
            })
        })
    })
}